<?php

defined('SYSPATH') or die('Access error!');

class Controller_User_Ajax extends Controller
{

    public function __construct(Request $request)
    {
        parent::__construct($request);
        header('Content-Type', 'application/json');
        if (!Request::$is_ajax)
        {
            $this->request->redirect(URL::site());
            return;
        }
    }

    public function action_delete_collect()
    {
        $id = Arr::get($_POST, 'id', '');
        if(empty($id)){
            echo 'error1';
            exit;
        }
        $collect_detail = Model::factory('collect')->get_row(array('id' => $id, 'user_id' => Session::instance()->get_detail('user','id')), 'type,id');
        if (!count($collect_detail) > 0)
        {
            echo 'error2';
            exit;
        }
        if (Model::factory('collect')->delete(array('id' => $id)))
        {
            //处理相关数据的数量
            $temp = Statics::instance()->get_table_by_type($collect_detail['type']);
            if (Arr::validate_arr($temp))
            {
                Statics::instance()->update_static($temp['table'], array($temp['key'] => $collect_detail['id']), 'collects', 'sub');
            }
            echo 'success';
            exit;
        }
    }

}